/**
 * @Authoer: hujun
 * @Description: 用户授权机构管理
 * @Date 2017/12/5 10:41
 * @Modified By:
 *
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/userGrantOrgManager/userGrantOrgManager.js',
  './custom/widgets/js/yufpOrgTree.js', // 机构放大镜
  './custom/widgets/js/yufpUserSelector.js'// 用户放大镜
],
function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    // 注册该功能要用到的数据字典
    yufp.lookup.reg('DATA_STS,SYS_TYPE');
    // 创建virtual model
    yufp.custom.vue({
      el: '#userGrantOrg',
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;
        return {
          height: yufp.custom.viewSize().height - 150,
          queryFields: [
            {placeholder: this.$t('userGrantOrgManager.jgdm'), field: 'orgId', type: 'custom', is: 'yufp-org-tree'},
            {placeholder: this.$t('userGrantOrgManager.yhmc'), field: 'userId', type: 'custom', is: 'yufp-user-selector'},
            {placeholder: this.$t('userGrantOrgManager.zt'), field: 'relSts', type: 'select', dataCode: 'DATA_STS'}
          ],
          queryButtons: [
            {
              label: this.$t('userGrantOrgManager.cx'),
              op: 'submit',
              type: 'primary',
              click: function (model, valid) {
                if (valid) {
                  var param = { condition: JSON.stringify(model) };
                  me.$refs.grantTable.remoteData(param);
                }
              }
            },
            {label: this.$t('userGrantOrgManager.zz'), op: 'reset'}
          ],
          mainGrid: {
            query: {
              orgId: '',
              userId: '',
              relSts: ''
            },
            height: yufp.custom.viewSize().height - 220,
            checkbox: true,
            dataUrl: backend.appOcaService + '/api/adminsmusermgrorg/querypage',
            paging: {
              page: 1,
              size: 10
            },
            currentRow: null,
            tableColumns: [
              { label: this.$t('userGrantOrgManager.yhmc'),
                prop: 'userName',
                width: 200,
                resizable: true,
                template: function () {
                  return '<template scope="scope">\
                                <a href="javascipt:void(0);" style="text-decoration:underline;" @click="_$event(\'custom-detail-click\', scope)">{{ scope.row.instuName }}</a>\
                            </template>';
                } },
              { label: this.$t('userGrantOrgManager.jgmc'), prop: 'orgName', width: 180, resizable: true },
              { label: this.$t('userGrantOrgManager.zt'), prop: 'relSts', width: 120, resizable: true, dataCode: 'DATA_STS'},
              { label: this.$t('userGrantOrgManager.zxbgyh'), prop: 'lastChgName', width: 150, resizable: true},
              { label: this.$t('userGrantOrgManager.zxbgsj'), prop: 'lastChgDt', type: 'date', resizable: true}
            ]
          },
          updateFields: [{
            columnCount: 2,
            fields: [
              { field: 'userId',
                type: 'custom',
                is: 'yufp-user-selector',
                label: this.$t('userGrantOrgManager.yhmc'),
                rules: [
                  {required: true, message: this.$t('userGrantOrgManager.btx'), trigger: 'change'}
                ]},
              { field: 'orgId',
                type: 'custom',
                label: this.$t('userGrantOrgManager.jgmc'),
                is: 'yufp-org-tree',
                param: {needCheckbox: true},
                rules: [
                  {required: true, message: this.$t('userGrantOrgManager.btx'), trigger: 'change'}
                ] },
              { field: 'relSts', label: this.$t('userGrantOrgManager.zt'), type: 'select', dataCode: 'DATA_STS', disabled: true }
            ]
          }],
          dialogFormVisible: false,
          dialogStatus: '',
          formDisabled: false,
          textMap: {
            update: this.$t('userGrantOrgManager.xg'),
            detail: this.$t('userGrantOrgManager.xq'),
            create: this.$t('userGrantOrgManager.xz')
          }
        };
      },
      mounted: function () {
        this.queryFn();
      },
      methods: {
        queryFn: function () {
          var me = this;
          me.$refs.grantTable.remoteData();
        },
        openCreateFn: function () {
          this.opType('create', false);
          this.$nextTick(function () {
            this.$refs.grantInfoform.resetFields();
            this.$refs.grantInfoform.formModel.relSts = 'W';
          });
        },
        openDetailFn: function (row) {
          this.opType('detail', true);
          this.$nextTick(function () {
            this.$refs.grantInfoform.resetFields();
            yufp.extend(this.$refs.grantInfoform.formModel, row.row);
          });
        },
        openEditFn: function () {
          if (this.$refs.grantTable.selections.length !== 1) {
            this.$message({message: this.$t('userGrantOrgManager.qxzytsj'), type: 'warning'});
            return false;
          }
          var row = this.$refs.grantTable.selections[0];
          if (row.instuSts === 'A') {
            this.$message({message: this.$t('userGrantOrgManager.znxztyhdqydsj'), type: 'warning'});
            return;
          }
          this.opType('update', false);
          this.$nextTick(function () {
            this.$refs.grantInfoform.resetFields();
            yufp.extend(this.$refs.grantInfoform.formModel, row);
          });
        },
        opType: function (type, disabled) {
          this.dialogFormVisible = true;
          this.dialogStatus = type;
          this.formDisabled = disabled;
        },
        saveCreateFn: function () {
          var em = this;
          var form = em.$refs.grantInfoform;
          form.validate(function (valid) {
            if (valid) {
              var comitData = {};
              delete form.formModel.instuId;
              yufp.extend(comitData, form.formModel);
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmusermgrorg/addusermgrorg',
                data: comitData,
                callback: function (code, message, response) {
                  em.dialogFormVisible = false;
                  em.$message({message: em.$t('userGrantOrgManager.sjbccg')});
                  em.queryFn();
                }
              });
            } else {
              em.$message({message: em.$t('userGrantOrgManager.qjcsrxsfhf'), type: 'warning'});
              return false;
            }
          });
        },
        saveEditFn: function () {
          var vue = this;
          var form = vue.$refs.grantInfoform;
          form.validate(function (valid) {
            if (valid) {
              var comitData = {};
              yufp.extend(comitData, form.formModel);
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmusermgrorg/update',
                data: comitData,
                callback: function (code, message, response) {
                  vue.dialogFormVisible = false;
                  vue.$message({message: vue.$t('userGrantOrgManager.sjbccg')});
                  vue.queryFn();
                }
              });
            } else {
              this.$message({message: this.$t('userGrantOrgManager.qjcsrxsfhf'), type: 'warning'});
              return false;
            }
          });
        },
        useFn: function () { // 启用
          if (this.$refs.grantTable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.grantTable.selections.length; i++) {
              var row = this.$refs.grantTable.selections[i];
              if (row.relSts !== 'A') {
                id = id + ',' + row.userMgrOrgId;
              } else {
                this.$message({message: this.$t('userGrantOrgManager.znxztyhzdsxdsj'), type: 'warning'});
                return false;
              }
            }
            var vue = this;
            yufp.service.request({
              method: 'POST',
              url: backend.appOcaService + '/api/adminsmusermgrorg/usebatch',
              data: {
                id: id
              },
              callback: function (code, message, response) {
                vue.$message({message: vue.$t('userGrantOrgManager.sjczcg')});
                vue.queryFn();
              }
            });
          } else {
            this.$message({message: this.$t('userGrantOrgManager.qxxzysxdsj'), type: 'warning'});
            return;
          }
        },
        unUseFn: function () { // 停用
          if (this.$refs.grantTable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.grantTable.selections.length; i++) {
              var row = this.$refs.grantTable.selections[i];
              if (row.relSts === 'A') {
                id = id + ',' + row.userMgrOrgId;
              } else {
                this.$message({message: this.$t('userGrantOrgManager.znxzsxdsj'), type: 'warning'});
                return false;
              }
            }
            var vue = this;
            yufp.service.request({
              method: 'POST',
              url: backend.appOcaService + '/api/api/adminsmusermgrorg/unusebatch',
              data: {
                id: id
              },
              callback: function (code, message, response) {
                vue.$message({message: vue.$t('userGrantOrgManager.sjczcg')});
                vue.queryFn();
              }
            });
          } else {
            this.$message({message: this.$t('userGrantOrgManager.qxxzysxdsj'), type: 'warning'});
            return false;
          }
        },
        deletestFn: function () { // 删除
          if (this.$refs.grantTable.selections.length > 0) {
            var id = '';
            for (var i = 0; i < this.$refs.grantTable.selections.length; i++) {
              var row = this.$refs.grantTable.selections[i];
              if (row.relSts !== 'A') {
                id = id + ',' + row.userMgrOrgId;
              } else {
                this.$message({message: this.$t('userGrantOrgManager.znscsxhzdsxdsj'), type: 'warning'});
                return false;
              }
            }
            var vue = this;
            this.$confirm(this.$t('userGrantOrgManager.cczjyjscgwjsfjx'), this.$t('userGrantOrgManager.ts'), {
              confirmButtonText: this.$t('userGrantOrgManager.qd'),
              cancelButtonText: this.$t('userGrantOrgManager.qx'),
              type: 'warning',
              center: true
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                url: backend.appOcaService + '/api/adminsmusermgrorg/deletebatch',
                data: {
                  id: id
                },
                callback: function (code, message, response) {
                  vue.$message({message: vue.$t('userGrantOrgManager.sjsccg')});
                  vue.queryFn();
                }
              });
            });
          } else {
            this.$message({message: this.$t('userGrantOrgManager.qxxzyscdsj'), type: 'warning'});
            return false;
          }
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) {


  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
